package testing;
import processing.core.*;
import processing.xml.*;

import java.applet.*;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.MouseEvent;
import java.awt.event.KeyEvent;
import java.awt.event.FocusEvent;
import java.awt.Image;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import java.util.zip.*;
import java.util.regex.*;

public class Spring {

	/**
	 * Springs.
	 * 
	 * Move the mouse over one of the circles and click to re-position. When you
	 * release the mouse, it will snap back into position. Each circle has a
	 * slightly different behavior.
	 */
	
	// here teach u how to use it
	// (1) draw --- update+ display: 
	
	/**
	 * for instance: 
	 * public void display() { if (over) { fill(153); } else { fill(255); }
	 * ellipse(tempxpos, tempypos, size, size); }
	 */
	
	// (2) mousepressed --->pressed
	// (3) mousereleased ----> release

		// Screen values
		float xpos, ypos;
		float tempxpos, tempypos;
		int size = 20;
		boolean over = false;
		boolean move = false;

		// Spring simulation constants
		float mass; // Mass
		float k = 0.2f; // Spring constant
		float damp; // Damping
		float rest_posx; // Rest position X
		float rest_posy; // Rest position Y

		// Spring simulation variables
		// float pos = 20.0; // Position
		float velx = 0.0f; // X Velocity
		float vely = 0.0f; // Y Velocity
		float accel = 0; // Acceleration
		float force = 0; // Force

		Spring[] friends;
		int me;
		
		//mouse move
		int mouseX;
		int mouseY;

		// Constructor
		Spring(float x, float y, int s, float d, float m, float k_in,
				Spring[] others, int id) {
			
			
			xpos = tempxpos = x;
			ypos = tempypos = y;
			rest_posx = x;
			rest_posy = y;
			size = s;
			damp = d;
			mass = m;
			k = k_in;
			friends = others;
			me = id;
		}

		public Spring() {
			// TODO Auto-generated constructor stub
		}

		public void update() {
			if (move) {
				rest_posy = mouseY;
				rest_posx = mouseX;
			}

			force = -k * (tempypos - rest_posy); // f=-ky
			accel = force / mass; // Set the acceleration, f=ma == a=f/m
			vely = damp * (vely + accel); // Set the velocity
			tempypos = tempypos + vely; // Updated position

			force = -k * (tempxpos - rest_posx); // f=-ky
			accel = force / mass; // Set the acceleration, f=ma == a=f/m
			velx = damp * (velx + accel); // Set the velocity
			tempxpos = tempxpos + velx; // Updated position

			if ((over() || move) ) {
				over = true;
			} else {
				over = false;
			}
		}

		// Test to see if mouse is over this spring
		public boolean over() {
			float disX = tempxpos - mouseX;
			float disY = tempypos - mouseY;
			if ((Math.sqrt(disX*disX + disY*disY)) < size / 2) {
				return true;
			} else {
				return false;
			}
		}

		// Make sure no other springs are active
		//we will do this, but not here
		/**
		public boolean otherOver() {
			for (int i = 0; i < num; i++) {
				if (i != me) {
					if (friends[i].over == true) {
						return true;
					}
				}
			}
			return false;
		}
		*/

		public void pressed() {
			if (over) {
				move = true;
			} else {
				move = false;
			}
		}

		public void released(int mousex, int mousey) {
			
			this.mouseX = mousex;
			this.mouseY = mousex;
			move = false;
			rest_posx = mouseX;
			rest_posy = mouseY;
		}
	

}
